package com.xcity.game.activity.newplayer.tracer;

import java.text.MessageFormat;

import com.xcity.db.entity.Employee;
import com.xcity.game.activity.newplayer.NewPlayerDoingStr;
import com.xcity.game.activity.newplayer.NewPlayerTask;
import com.xcity.game.activity.newplayer.NewPlayerTracer;
import com.xcity.game.activity.newplayer.NewPlayerTracerLabel;
import com.xcity.game.employee.template.EmployeeQualityUpStringTemplate;

import naga.x.App;
import naga.x.game.template.TemplateService;

@NewPlayerTracerLabel(NewPlayerTracer.EMPLOYEE_QUALITY)
public class EmployeeQualityTracer implements NewPlayerTracer {

	@Override
	public boolean isDone(NewPlayerTask task) {
		int[] params = task.getTemplate().getParam();
		int[] progress = task.getProgress();
		if(params[0] == 1){
			if(progress[0] == 1 && progress[1] >= params[1]){
				return true;
			}
		}else{
			if(progress[0] >= params[0] && progress[1] >= params[1]){
				return true;
			}
		}
		return false;
	}

	@Override
	public void onChange(NewPlayerTask task, Object... vals) {
		int oldQuality = (int)vals[2];
		int newQuality = 0;
		Employee e = (Employee) vals[1];
		if(e != null){
			newQuality = e.getQualityLvl();
		}
		int[] params = task.getTemplate().getParam();
		int[] progress = task.getProgress();
		if(params[0] == 1){
			//统计等级
			if(oldQuality < params[1] && newQuality >= params[1]){
				progress[0] = 1;
			}
			if(newQuality > progress[1]){
				progress[1] = newQuality;
			}
		}else{
			//员工数
			if(oldQuality < params[1] && newQuality >= params[1]){
				progress[0] = progress[0] + 1;
			}
			if(progress[0] == 0){
				progress[1] = newQuality;
			}else{
				progress[1] = params[1];
			}
		}
		if(progress[0] > PARAM0_MAX){
			progress[0] = PARAM0_MAX;
		}
		if(progress[1] > PARAM1_MAX){
			progress[1] = PARAM1_MAX;
		}
	}

	@Override
	public String getDoingStr(NewPlayerTask task) {
		int[] params = task.getTemplate().getParam();
		String qualityStr = "";
//		//TODO 员工品质
//		switch (params[1]) {
//		default:
//			qualityStr = "D级";
//			break;
//		}
		EmployeeQualityUpStringTemplate temp = App.getApp().getServiceManager().get(TemplateService.class).get(EmployeeQualityUpStringTemplate.class, params[1]);
		if(temp != null){
			qualityStr = temp.getStr();
		}
		if(params[0] == 1){
			return MessageFormat.format(NewPlayerDoingStr.STR_EMPLOYEE_QUALITY, qualityStr);
		}
		return MessageFormat.format(NewPlayerDoingStr.STR_EMPLOYEE_QUALITY_MULTI, params[0], qualityStr);
	}

}
